package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pojo.Commodity;
import com.pojo.Sell;
import com.service.CommodityService;
import com.service.IndentService;
import com.util.LayuiTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Controller
@Transactional
@RequestMapping("/comm")
public class CommodityController {
    @Autowired
    public CommodityService commodityService;
    @Autowired
    public IndentService indentService;


//    库存列表
    @RequestMapping("list")
    @ResponseBody
    public String commList(Integer cid,String cname,String ctype){
        return JSON.toJSONString(LayuiTable.tableData(commodityService.commodityFindAll(cid, cname, ctype).size(),
                commodityService.commodityFindAll(cid, cname, ctype)), SerializerFeature.DisableCircularReferenceDetect);
    }

    //商品列表
    @RequestMapping("/sellList")
    @ResponseBody
    public String sellList(){
        return JSON.toJSONString(LayuiTable.tableData(commodityService.sellSelect(null,null).size(),
                commodityService.sellSelect(null,null)), SerializerFeature.DisableCircularReferenceDetect);
    }

    //    商品售价修改
    @RequestMapping("/updSprice")
    @ResponseBody
    public String commodityUpdSprice(@RequestParam("cid") Integer cid, @RequestParam("sprice") BigDecimal sprice, HttpSession session) {
        Integer empno= (Integer) session.getAttribute("loginEmpno");
        if (commodityService.commodityFindByCid(cid).get(0).getSprice()!=null){
            if (commodityService.commodityUpdSprice(cid, sprice,empno)==1&&commodityService.sellUpdSprice(cid, sprice,empno)==1){
                return "true";
            }else {
                return "false";
            }
        }else {
            if (commodityService.commodityUpdSprice(cid, sprice,empno)==1){
                Integer sid=1;
                List<Map<String,Object>> sells=commodityService.sellSelect(null,null);
                if (sells.size()!=0){
                    sid= ((Integer) sells.get(sells.size()-1).get("sid"))+1;
                }
                Commodity commoditys=commodityService.commodityFindByCid(cid).get(0);
                Integer snum=commoditys.getCount()*commoditys.getMids().getScale();
                Sell sell=new Sell(sid,cid,sprice,Double.valueOf(snum));
                int b= commodityService.sellInsert(sell);
                if (b==1){
                    return "true";
                }else {
                    return "false";
                }
            }else {
                return "false";
            }
        }
    }

    //    货架数量修改库存数量
    @RequestMapping("/sellUpdCount")
    @ResponseBody
    public String sellUpdCount() {
        int a=0;
        int b=0;
        for (Map<String,Object> sellList:commodityService.sellSelect(null,null)
             ) {
                    Integer cid= (Integer) sellList.get("cid");
                    double snum1=((Double) sellList.get("snum"));
                    int snum= (int) snum1;
                    Integer scale= (Integer) sellList.get("scale");
                    Integer remainder=snum%scale;
                    Integer count=(snum-remainder)/scale;
                    if (remainder!=0){
                        count+=1;
                    }
                    a=commodityService.commodityUpdCount(cid,count);
                    if (a==1){
                        b+=1;
                    }
        }
       return "true";
    }
}
